﻿using HARTCalibrationTool.SerialPortManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HARTCalibrationTool.Commands.BatchCalibrationBoard
{
    public enum SolenoidValveStatus
    { 
        eSolenodValueClose = 0x00,
        eSolenodValueOpen = 0x01,

        eNegPreSolenodValueClose = 0x02,
        eNegPreSolenodValueOpen = 0x03,
    }

    public class BatchCaliBoardCMDsSolenoidValve : BatchCaliBoardCMDsBase
    {
        private SolenoidValveStatus _status;

        public BatchCaliBoardCMDsSolenoidValve(SerialPortsBase serialPortManager, string commandName, SolenoidValveStatus status) : base(serialPortManager, commandName)
        {
            _status = status;
        }

        protected override async Task SendCMD()
        {
            byte[] cmdBytes = new byte[8] { 0xAA, 0x55, 0x04, 0x01, 0x00, 0x00, 0x0D, 0x0A };

            cmdBytes[4] = (byte)_status;
            cmdBytes[5] = (byte)(cmdBytes[0] + cmdBytes[1] + cmdBytes[2] + cmdBytes[3] + cmdBytes[4]);

            SerialPortsBase.PrintMessage("--> " + Utils.BytesArrayToHexString(cmdBytes, suffix: " "));

            await SerialPortMager.SerialPortWrite(cmdBytes);
        }

        public override async Task ParseResponse(byte[] data)
        {
            await Task.Run(() => {
                SerialPortsBase.PrintMessage("<-- " + $"{Utils.HexToString(data)}");
            });
        }

        public override void OnFailure()
        {

        }

        public override void OnSuccess()
        {

        }

        public override void OnTimeout()
        {

        }
    }
}
